/**
 * 
 */
package com.vivid.dynamics.oknow.dao.impl;

import java.io.Serializable;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.vivid.dynamics.oknow.dao.IPermissionDAO;
import com.vivid.dynamics.oknow.model.Permission;

/**
 *
 * @author jason
 * created at:: Aug 5, 2011 11:26:12 AM
 */
public class PermissionDAOImpl implements IPermissionDAO {

	static Logger logger=Logger.getLogger(PermissionDAOImpl.class);
	
	private @Autowired SessionFactory sessionFactory;
	
		
	/**
	 * {@inheritDoc}
	 * @see com.vivid.dynamics.oknow.dao.IPermissionDAO#getPermissionByKey(int)
	 */
	@Override
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public Permission getPermissionByKey(int pId) {
		Session aSession=this.sessionFactory.getCurrentSession();
		Permission aPer=null;
		
		aPer=(Permission) aSession.get(Permission.class, Integer.valueOf(pId));
		
		return aPer;
	}

	/**
	 * {@inheritDoc}
	 * @see com.vivid.dynamics.oknow.dao.IPermissionDAO#newPermission(com.vivid.dynamics.oknow.model.Permission)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public Serializable newPermission(Permission pPermission) {
		Session aSession=this.sessionFactory.getCurrentSession();
		Serializable aKey=null;
		
		if (pPermission!=null) {
			aKey=aSession.save(pPermission);
			if (logger.isDebugEnabled()) {
				logger.debug("[newPermission]: "+aKey);
			}
		}
		return aKey;
	}

	/**
	 * {@inheritDoc}
	 * @see com.vivid.dynamics.oknow.dao.IPermissionDAO#updatePermission(com.vivid.dynamics.oknow.model.Permission)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void updatePermission(Permission pPermission) {
		Session aSession=this.sessionFactory.getCurrentSession();
		
		if (pPermission!=null) {
			aSession.update(pPermission);
		}
	}

	/**
	 * {@inheritDoc}
	 * @see com.vivid.dynamics.oknow.dao.IPermissionDAO#deletePermission(com.vivid.dynamics.oknow.model.Permission)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void deletePermission(Permission pPermission) {
		Session aSession=this.sessionFactory.getCurrentSession();
		
		if (pPermission!=null) {
			aSession.delete(pPermission);
		}
	}

}
